package com.cskaoyan.gateway.controller.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.JwtTokenUtils;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.CreateOrderRequest;
import com.mall.order.dto.CreateOrderResponse;
import com.mall.order.dto.OrderListRequest;
import com.mall.order.dto.OrderListResponse;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author 谭涛
 */
@RequestMapping("shopping")
@RestController
public class OrderController {

    @Reference(timeout = 2000, retries = 0, check = false)
    OrderQueryService orderQueryService;

    @Reference(timeout = 2000, retries = 0, check = false)
    OrderCoreService orderCoreService;

    @Anoymous
    @GetMapping("order")
    public ResponseData orderList(@RequestParam(value = "page",defaultValue = "1",required = false) Integer page,
                                  @RequestParam(value = "size",defaultValue = "5",required = false) Integer size,
                                  @RequestParam(value = "page",required = false) String sort,
                                  HttpServletRequest request){

        /*String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        long uid = Long.parseLong(userInfo);*/
        String userInfo = null;
        Long uid = null;
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(TokenIntercepter.ACCESS_TOKEN)) {
                userInfo = JwtTokenUtils.builder().token(cookie.getValue()).build().freeJwt();
                uid = Long.parseLong(userInfo);
            }
        }

//        long uid = 62;
        OrderListRequest orderListRequest = new OrderListRequest();
        orderListRequest.setUserId(uid);
        orderListRequest.setPage(page);
        orderListRequest.setSize(size);
        orderListRequest.setSort(sort);
        OrderListResponse response = orderQueryService.orderList(orderListRequest);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            Map<String, Object> map = new HashMap<>();
            map.put("total",response.getTotal());
            map.put("data",response.getDetailInfoList());
            return new ResponseUtil().setData(map);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @PostMapping("order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest createOrderRequest, HttpServletRequest request) {
        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        long uid = Long.parseLong(userInfo);
        createOrderRequest.setUserId(uid);
        createOrderRequest.setUniqueKey(UUID.randomUUID().toString());
        CreateOrderResponse createOrderResponse = orderCoreService.createOrder(createOrderRequest);
        if (createOrderResponse.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(createOrderResponse.getOrderId());
        }
        return new ResponseUtil<>().setErrorMsg(createOrderResponse.getMsg());
    }


}
